import { Vector2D } from '../utils/Vector2D';

export type Weather = 'clear' | 'rain' | 'snow';
export type TimeOfDay = 'dawn' | 'day' | 'dusk' | 'night';

export interface Trap {
    position: Vector2D;
    type: 'spike' | 'portal' | 'slowField';
    active: boolean;
    duration: number;
}

export class EnvironmentManager {
    private currentTime: number = 0;
    private dayDuration: number = 300000; // 5分钟一个完整昼夜循环
    private currentWeather: Weather = 'clear';
    private weatherDuration: number = 60000; // 天气持续时间
    private weatherTimer: number = 0;
    private traps: Trap[] = [];
    private trapSpawnInterval: number = 15000; // 陷阱生成间隔
    private trapTimer: number = 0;

    constructor() {}

    public update(deltaTime: number, canvasWidth: number, canvasHeight: number): void {
        // 更新时间
        this.currentTime = (this.currentTime + deltaTime) % this.dayDuration;

        // 更新天气
        this.updateWeather(deltaTime);

        // 更新陷阱
        this.updateTraps(deltaTime, canvasWidth, canvasHeight);
    }

    private updateWeather(deltaTime: number): void {
        this.weatherTimer -= deltaTime;
        if (this.weatherTimer <= 0) {
            this.generateNewWeather();
            this.weatherTimer = this.weatherDuration;
        }
    }

    private generateNewWeather(): void {
        const weathers: Weather[] = ['clear', 'rain', 'snow'];
        const currentIndex = weathers.indexOf(this.currentWeather);
        let newIndex;

        // 70%概率保持晴天
        if (this.currentWeather === 'clear' && Math.random() < 0.7) {
            return;
        }

        do {
            newIndex = Math.floor(Math.random() * weathers.length);
        } while (newIndex === currentIndex);

        this.currentWeather = weathers[newIndex];
    }

    private updateTraps(deltaTime: number, canvasWidth: number, canvasHeight: number): void {
        // 更新现有陷阱
        this.traps = this.traps.filter(trap => {
            trap.duration -= deltaTime;
            return trap.duration > 0;
        });

        // 生成新陷阱
        this.trapTimer -= deltaTime;
        if (this.trapTimer <= 0) {
            this.generateTrap(canvasWidth, canvasHeight);
            this.trapTimer = this.trapSpawnInterval;
        }
    }

    private generateTrap(canvasWidth: number, canvasHeight: number): void {
        const trapTypes: Trap['type'][] = ['spike', 'portal', 'slowField'];
        const type = trapTypes[Math.floor(Math.random() * trapTypes.length)];
        
        const trap: Trap = {
            position: new Vector2D(
                Math.random() * (canvasWidth - 40) + 20,
                Math.random() * (canvasHeight - 40) + 20
            ),
            type,
            active: true,
            duration: 10000 // 陷阱持续10秒
        };

        this.traps.push(trap);
    }

    public reset(): void {
        this.currentTime = 0;
        this.currentWeather = 'clear';
        this.weatherTimer = this.weatherDuration;
        this.trapTimer = this.trapSpawnInterval;
        this.traps = [];
    }

    public getTimeOfDay(): TimeOfDay {
        const timeProgress = this.currentTime / this.dayDuration;
        if (timeProgress < 0.25) return 'dawn';
        if (timeProgress < 0.5) return 'day';
        if (timeProgress < 0.75) return 'dusk';
        return 'night';
    }

    public getWeather(): Weather {
        return this.currentWeather;
    }

    public getTraps(): Trap[] {
        return this.traps;
    }

    public getFoodSpawnRate(): number {
        // 根据时间和天气调整食物生成概率
        let rate = 1.0;

        // 时间影响
        const timeOfDay = this.getTimeOfDay();
        switch (timeOfDay) {
            case 'night':
                rate *= 0.5; // 夜晚食物较少
                break;
            case 'dawn':
            case 'dusk':
                rate *= 0.8; // 黎明和黄昏食物略少
                break;
        }

        // 天气影响
        switch (this.currentWeather) {
            case 'rain':
                rate *= 1.2; // 雨天食物较多
                break;
            case 'snow':
                rate *= 0.7; // 雪天食物较少
                break;
        }

        return rate;
    }

    public getControlSensitivity(): number {
        // 根据天气调整控制灵敏度
        switch (this.currentWeather) {
            case 'rain':
                return 0.8; // 雨天降低灵敏度
            case 'snow':
                return 0.6; // 雪天显著降低灵敏度
            default:
                return 1.0;
        }
    }
}